/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sinaLogin;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import sinaLogin.com.com.sinaweibo.SinaLogin;

/**
 *
 * @author Administrator
 */
public class sinaLogin {

    public static void main(String[] args) throws IOException {
        HttpClient httpClient = new DefaultHttpClient();
        String account = "kaldr@sina.cn";
        String password = "nevergiveup";
        SinaLogin sinalogin = new SinaLogin((DefaultHttpClient) httpClient, account, password);
        sinalogin.connect();
        String cookie = sinalogin.getCookieString();
        String cookieSeries[] = cookie.split(";");
        //String url = "http://photo.weibo.com/2293429280/albums/detail/album_id/41764353";
        String url = "http://photo.weibo.com/albums/ajaxPhotosList?album_id=47052653&uid=2276712484";
        HttpGet get = new HttpGet(url);
        get.setHeader("Cookie", cookie);
        HttpResponse response = httpClient.execute(get);
        HttpEntity entity = response.getEntity();
        String oText = EntityUtils.toString(entity);
        String alink = "imgBSrc\":\"([^\"]+)\",\"thumbnail_pic";
        Matcher m = Pattern.compile(alink).matcher(oText);
        List<String> urls = new ArrayList<String>();
        while (m.find()) {
            String http = m.group(1).replace("\\", "");
            System.out.println(http);
            urls.add(http);
        }
        
        String path = "D:\\Pictures\\beauty\\";
        File f=new File(path);
        if(f.isDirectory()){
             File[] codefile = f.listFiles();
             String foldername=codefile[codefile.length-1].getName();
             int nameInteger=Integer.parseInt(foldername)+1;
             String name="";
             if (nameInteger < 10) {
                name = "00" + nameInteger + "\\";
            } else if (nameInteger < 100) {
                name = "0" + nameInteger + "\\";
            } else {
                name = nameInteger + "" + "\\";
            }
            path=path+name;
        }
        f=new File(path);
        f.mkdir();
        int nameInteger = 0;
        for (String u : urls) {
            String name = "";
            nameInteger += 1;
            if (nameInteger < 10) {
                name = "00" + nameInteger + ".jpg";
            } else if (nameInteger < 100) {
                name = "0" + nameInteger + ".jpg";
            } else {
                name = nameInteger + "" + ".jpg";
            }
            get = new HttpGet(u);
            get.setHeader("Cookie", cookie);
            HttpResponse hr = httpClient.execute(get);            
            InputStream in = hr.getEntity().getContent();
            writeImageLocal(path+name,loadImageUrl(u));
            get.abort();
        }
    }

    public BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 导入网络图片到缓冲区
     */
    public static BufferedImage loadImageUrl(String imgName) {
        try {
            URL url = new URL(imgName);
            return ImageIO.read(url);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     * 生成新图片到本地
     */
    public static void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
